package com.liuyfly.leetcode.questions;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangfang at 2022-09-06 18:37
 */
@Slf4j
public class Question0029 {
    public static int divide(int dividend, int divisor) {
        boolean flag = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0);
        if (dividend == 0) {
            return 0;
        }
        if (dividend == Integer.MIN_VALUE) {
            if (divisor == 1) {
                return Integer.MIN_VALUE;
            } else if (divisor == -1) {
                return Integer.MAX_VALUE;
            }
        }
        if (divisor == Integer.MIN_VALUE) {
            if (dividend == Integer.MIN_VALUE) {
                return 1;
            } else {
                return 0;
            }
        }
        dividend = dividend>0?dividend:dividend*-1;
        divisor = divisor>0?divisor:divisor*-1;
        int middle = dividend / 2;
        int myMultiplyResult = myMultiply(middle, divisor);
        if (myMultiplyResult < dividend && (myMultiplyResult + 1) > dividend) {
            return flag ? middle : middle * -1;
        } else if (myMultiplyResult > dividend) {
            return divide(middle, divisor);
        } else if ((myMultiplyResult + 1) < dividend) {
            return divide(middle, divisor);
        }
        return -1;
    }

    public static int myMultiply(int x, int y) {
        if (y == 1) {
            return x;
        }
        if (y == -1) {
            return x * -1;
        }
        int middle = y >>1;
        if (y % 2 == 0) {
            return myMultiply(x + x, middle);
        } else {
            return y > 0 ? (x + myMultiply(x + x, middle)) : (x * -1 + myMultiply(x + x, middle));
        }
    }

    public static int divide_123(int dividend, int divisor) {
        int originDivisor = divisor;
        boolean flag = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0);
        if (dividend < 0) {
            dividend = dividend * -1;
        }
        if (divisor < 0) {
            divisor = divisor * -1;
        }
        if (divisor == 1) {
            if (originDivisor == 1) {
                if (dividend == Integer.MIN_VALUE) {
                    return Integer.MIN_VALUE;
                }
            } else if (originDivisor == -1) {
                if (dividend == Integer.MIN_VALUE) {
                    return Integer.MAX_VALUE;
                }
            }
            if (flag) {
                return dividend;
            } else {
                return dividend * -1;
            }
        }
        int times = 0;
        if (dividend > 0) {
            while (dividend > 0) {
                dividend -= divisor;
                times++;
            }
        } else {
            while (dividend < 0) {
                dividend += divisor;
                times++;
            }
        }
        if (dividend != 0) {
            times--;
        }
        if (flag) {
            return times;
        } else {
            return times * -1;
        }
    }

    public static void main(String[] args) {
        int a = 10, b = -3;
        log.info("{}/{}={}", a, b, divide(a, b));
        log.info("max:{},min:{}", Integer.MAX_VALUE, Integer.MIN_VALUE);
    }
}
